package com.lire.shop.service.impl;

import com.lire.shop.dao.UserRepository;
import com.lire.shop.data.Constants;
import com.lire.shop.data.NullObjects;
import com.lire.shop.entity.User;
import com.lire.shop.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpSession;

@Service
public class UserServiceImpl implements UserService {

    @Autowired
    private UserRepository userRepository;


    public User getCurrentUser(HttpSession session){
        User curUser = (User) session.getAttribute(Constants.CURRENT_USER);
        return userRepository
                .findByUsername(curUser.getUsername())
                .orElseGet(NullObjects::getUser);
    }
}
